//
//  EditAndAddDelTableViewController.m
//  Utils
//
//  Created by enghou on 2018/6/7.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "EditAndAddDelTableViewController.h"
#import "NetWorkerManager.h"
#import "LocalPersistent.h"
@interface EditAndAddDelTableViewController ()

@end

@implementation EditAndAddDelTableViewController

-(NSString *)orgId{
    return [[LocalPersistent sharedInstance]contentForKey:@"orgId"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.ids length] == 0) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(addRecord)];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    }else{
        UIBarButtonItem *trash = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(delRecord)];
        trash.tintColor = [UIColor whiteColor];
        UIBarButtonItem *edit = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(updateRecord)];
        edit.tintColor = [UIColor whiteColor];
        self.navigationItem.rightBarButtonItems = @[trash,edit];
        [self loadData];
    }
    // Do any additional setup after loading the view.
}

-(void)beginLoading{
    [SVProgressHUD show];
}

-(void)endLoading{
    [SVProgressHUD dismiss];
}

-(void)alertSome:(NSString *)msg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });
}

-(void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)loadData{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:self.detailPath parameter:self.detailParam completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            if (self.configView) {
                self.configView(source);
            }
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(NSDictionary *)addedParameter{
    return nil;
}

-(void)addRecord{
    NSAssert([self.addPath length] > 0, @"请检查self.addPath字段");
    if (!self.addedParameter) {
        return;
    }
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:self.addPath parameter:self.addedParameter completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            [[NSNotificationCenter defaultCenter]postNotificationName:self.refresh_not_name object:nil];
            [self pop];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(NSDictionary *)updateParameter{
    return nil;
}

-(void)updateRecord{
    NSAssert([self.updatePath length] > 0, @"请检查self.updatePath字段");
    if (!self.updateParameter) {
        return;
    }
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:self.updatePath parameter:self.updateParameter completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            [[NSNotificationCenter defaultCenter]postNotificationName:self.refresh_not_name object:nil];
            [self pop];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(NSDictionary *)delParameter{
    return nil;
}

-(void)delRecord{
    NSAssert([self.delPath length] > 0, @"请检查self.delPath字段");
    if (!self.delParameter) {
        return;
    }
    [[NetWorkerManager sharedInstance]postWithPath:self.delPath parameter:self.delParameter completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            [[NSNotificationCenter defaultCenter]postNotificationName:self.refresh_not_name object:nil];
            [self pop];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

@end
